﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using System.Text;
using System.IO;

namespace website.template_faa.admin
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    public class getTHLXGXByJC : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string text = GetJsonData();
            context.Response.Write(text);
        }

        public string GetJsonData()
        {
            string sql = "select gxLeftId, gxRightTable, gxRightId from [tonghangLXGX] where gxLeftTable='tonghangJCLX' order by gxLeftId asc";
            System.Data.SqlClient.SqlDataReader reader = jsm.SqlHelper.SqlServerHelper.ExecuteReader(sql);

            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            int tempLeftId = -1;
            int tempId = -1;
            string tempName = null;
            using (JsonWriter writer = new JsonTextWriter(sw))
            {
                writer.Formatting = Formatting.Indented;
                writer.WriteStartObject();
                //writer.WritePropertyName("total");
                //writer.WriteValue(10);
                writer.WritePropertyName("rows");
                writer.WriteStartArray();
                while (reader.Read())
                {
                    if (tempLeftId != (int)reader["gxLeftId"])
                    {//每一行增加开始前缀
                        if (tempLeftId != -1)
                        {//每一行增加结束后缀
                            writer.WriteEnd();
                        }

                        tempLeftId = (int)reader["gxLeftId"];
                        tempName = dbcaac.DBHelper.GetNameById("tonghangJCLX", "jclxId", "jclxName", tempLeftId);
                        writer.WriteStartObject();
                        writer.WritePropertyName("jclxId");
                        writer.WriteValue(tempLeftId);
                        writer.WritePropertyName("jclxName");
                        writer.WriteValue(tempName);
                    }

                    if (reader["gxRightTable"].Equals("tonghangJHCLX"))
                    {
                        tempId = (int)reader["gxRightId"];
                        tempName = dbcaac.DBHelper.GetNameById("tonghangJHCLX", "jhclxId", "jhclxName", tempId);
                        writer.WritePropertyName("jhclxName");
                        writer.WriteValue(tempName);
                    }
                    else if (reader["gxRightTable"].Equals("tonghangTHCLX"))
                    {
                        tempId = (int)reader["gxRightId"];
                        tempName = dbcaac.DBHelper.GetNameById("tonghangTHCLX", "thclxId", "thclxName", tempId);
                        writer.WritePropertyName("thclxName");
                        writer.WriteValue(tempName);
                    }
                    else if (reader["gxRightTable"].Equals("tonghangNBYJ"))
                    {
                        tempId = (int)reader["gxRightId"];
                        tempName = dbcaac.DBHelper.GetNameById("tonghangNBYJ", "nbyjId", "nbyjName", tempId);
                        writer.WritePropertyName("nbyjName");
                        writer.WriteValue(tempName);
                    }
                    else if (reader["gxRightTable"].Equals("tonghangNBEJ"))
                    {
                        tempId = (int)reader["gxRightId"];
                        tempName = dbcaac.DBHelper.GetNameById("tonghangNBEJ", "nbejId", "nbejName", tempId);
                        writer.WritePropertyName("nbejName");
                        writer.WriteValue(tempName);
                    }
                    else if (reader["gxRightTable"].Equals("tonghangNBSJ"))
                    {
                        tempId = (int)reader["gxRightId"];
                        tempName = dbcaac.DBHelper.GetNameById("tonghangNBSJ", "nbsjId", "nbsjName", tempId);
                        writer.WritePropertyName("nbsjName");
                        writer.WriteValue(tempName);
                    }
                    else
                    {
                        System.Console.Write("Error occur!");
                    }
                }
                writer.WriteEnd();
                writer.WriteEndObject();
            }
            return sb.ToString();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
